package com.xxx.video.mine.presenters;

import android.content.Context;

import androidx.annotation.NonNull;

import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.dbs.beans.SearchFriendHistoryBean;
import com.xxx.video.commen.dbs.beans.SearchHistoryBean;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.views.AddFriendExtraMessageDialog;
import com.xxx.video.mine.beans.SearchResultListBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class FindFriendResultListPresenter extends BasePresenter<IFindFriendResultListView> {

    public FindFriendResultListPresenter(@NonNull Context context, IFindFriendResultListView baseView) {
        super(context, baseView);
    }

    /**
     * search
     * @param searchContent key
     * @param page page
     */
    public void search(String searchContent, int page) {
        SearchFriendHistoryBean searchFriendHistoryBean = new SearchFriendHistoryBean.Builder()
                .createTime(System.currentTimeMillis())
                .searchWord(searchContent)
                .build();
        DbUtils.getInstance().addFriendSearchHistoryBean(searchFriendHistoryBean);
        NetWorkUtils.getInstance().searchFriends(searchContent, page, new CommonResponseListener<SearchResultListBean>() {
            @Override
            public void onSuccess(SearchResultListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initFriendList(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    public void addFriends(SearchResultListBean.ListBean listBean, int position) {
        AddFriendExtraMessageDialog addFriendExtraMessageDialog = new AddFriendExtraMessageDialog(context);
        addFriendExtraMessageDialog.show();
        addFriendExtraMessageDialog.setOnButtonClickListener(message -> NetWorkUtils.getInstance().addFriends(listBean.userId, listBean.username, listBean.nickName, listBean.picUrl, message, new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().addFriendSuccess(position);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        }));

    }
}
